/*
    Copyright (c) Guangzhou Data Control Network Technology Co., Ltd. All rights reserved.
    Licensed under the MIT License.
    contact:dcdev_founder@foxmail.com
*/
function move_node() {
    move_node.prototype.init = function (data, cb) {

        // 保存参数
        window[`_dc_drop_${data.container}`] = data;

        window[`_dc_drop_${data.container}_width`] = (data.mini_size && data.mini_size.width) ? parseInt(data.mini_size.width.replace("px", "")) : 0;
        window[`_dc_drop_${data.container}_height`] = (data.mini_size && data.mini_size.height) ? parseInt(data.mini_size.height.replace("px", "")) : 0;

        console.log('size', window[`_dc_drop_${data.container}_width`], window[`_dc_drop_${data.container}_height`])
        // 是否有拖动句柄（如标题栏）
        hasHandle = false;

        container = document.getElementById(data.container)
        handle = document.getElementById(data.handle)

        if (!(container)) {
            console.error("container or handle must be exit!")
        }

        container = $("#" + data.container);
        container.css("position", "absolute");

        // 没有可拖动句柄，则使用容器拖动
        if (handle) {
            hasHandle = true;
            handle = $("#" + data.handle);
        } else {
            handle = $("#" + data.container);
        }

        if (data.is_log_position) {
            // 读出之前的位置
            pos = localStorage[`pos_${data.container}`];
            if (pos) {
                pos = JSON.parse(pos);
                container.css("top", pos["top"]);
                container.css("left", pos["left"]);
                container.css("width", pos["width"]);
                container.css("height", pos["height"]);
            }
        }

        $(document).mousemove(function (e) {
            if (!!this.move) {
                var posix = !document.move_target ? {
                    'x': 0,
                    'y': 0
                } : document.move_target.posix,
                    callback = document.call_down || function () {
                        $(this.move_target).css({
                            'top': e.pageY - posix.y,
                            'left': e.pageX - posix.x
                        });
                    };

                // 最小尺寸处理
                // // console.log('size', window[`_dc_drop_${data.container}_width`], window[`_dc_drop_${data.container}_height`])
                // // console.log((e.pageX - posix.x), (e.pageY - posix.y))
                // j = 0
                // if (((e.pageX - posix.x) - window[`_dc_drop_${data.container}_left`]) > window[`_dc_drop_${data.container}_width`]) {
                //     // posix.x = window[`_dc_drop_${data.container}_width`]
                //     j = 1;
                //     console.log(e.pageX, e.pageY, posix, window[`_dc_drop_${data.container}_top`], window[`_dc_drop_${data.container}_left`]);
                // }
                // if (((e.pageY - posix.y) - window[`_dc_drop_${data.container}_top`]) > window[`_dc_drop_${data.container}_height`]) {
                //     // posix.y = window[`_dc_drop_${data.container}_height`]
                //     j = 1
                //     console.log(e.pageX, e.pageY, posix, window[`_dc_drop_${data.container}_top`], window[`_dc_drop_${data.container}_left`]);
                // }
                // if (j > 0) {
                //     callback.call(this, e, posix);
                // }

                callback.call(this, e, posix);

            }
        }).mouseup(function (e) {
            if (!!this.move) {
                var callback = document.call_up || function () { };
                callback.call(this, e);
                $.extend(this, {
                    'move': false,
                    'move_target': null,
                    'call_down': false,
                    'call_up': false
                });
            }
        });

        // 是否有拖动句柄
        drop_target = hasHandle ? `#${data.container} #${data.handle}` : `#${data.container}`;

        $(drop_target).bind({
            "mousedown": function (e) {
                // 有句柄则使用句柄的父节点，没有则使用本身
                var $p = hasHandle ? $(this).parent() : $(this);
                var $pp = $p[0];
                var offset = $p.offset();
                $pp.posix = {
                    'x': e.pageX - offset.left,
                    'y': e.pageY - offset.top
                };
                $.extend(document, {
                    'move': true,
                    'move_target': $pp
                });
            }
        });


        // var $box = $(drop_target).mousedown(function (e) {
        //     console.error(999)
        //     var $p = $(hasHandle?this:null).parent();
        //     var $pp = $p[0];
        //     var offset = $p.offset();
        //     $pp.posix = {
        //         'x': e.pageX - offset.left,
        //         'y': e.pageY - offset.top
        //     };
        //     $.extend(document, {
        //         'move': true,
        //         'move_target': $pp
        //     });
        // });

        $(`#${data.container}`).bind({
            'mousemove': function (e) {
                $(this).css({
                    cursor: "default"
                });
                var offset = $(this).offset(),
                    resize = true;
                var x = e.clientX,
                    y = e.clientY,
                    t = offset.top,
                    l = offset.left,
                    w = $(this).width(),
                    h = $(this).height(),
                    b = 5;

                // 区分是否有拖动块
                ww = hasHandle ? $(`#${data.handle}`).height() : $(`#${data.container}`).height();

                if (x < (l + b) && y > (t + ww) && y < (t + h - b)) {
                    $(this).css({
                        cursor: "w-resize"
                    });
                    $(this).unbind("mousedown").bind({
                        "mousedown": function (e) {
                            var $p = $(this);
                            var posix = {
                                'w': $p.width(),
                                'h': $p.height(),
                                'x': e.pageX,
                                'y': e.pageY
                            };

                            $.extend(document, {
                                'move': true,
                                'call_down': function (e) {
                                    $p.css({
                                        'width': Math.max(30, posix
                                            .x - e.pageX + posix
                                                .w),
                                        'left': Math.max(30, e
                                            .pageX)
                                    });
                                }
                            });
                        }
                    });
                } else if (x < (l + w) && x > (l + w - b) && y > (t + ww) && y < (t + h - b)) {
                    $(this).css({
                        cursor: "e-resize"
                    });
                    $(this).unbind("mousedown").bind({
                        "mousedown": function (e) {
                            var $p = $(this);
                            var posix = {
                                'w': $p.width(),
                                'x': e.pageX
                            };
                            resizeBox($p, posix, e);
                        }
                    });
                } else if (y < (t + h) && y > (t + h - b) && x > (l + b) && x < (l + w - b)) {
                    $(this).css({
                        cursor: "s-resize"
                    });
                    $(this).unbind("mousedown").bind({
                        "mousedown": function (e) {
                            var $p = $(this);
                            var posix = {
                                'h': $p.height(),
                                'y': e.pageY
                            };
                            resizeBox($p, posix, e);
                        }
                    });
                } else if (x < (l + b) && y > (t + h - b) && y < (t + h)) {
                    $(this).css({
                        cursor: "sw-resize"
                    });
                    $(this).unbind("mousedown").bind({
                        "mousedown": function (e) {
                            var $p = $(this);
                            var posix = {
                                'w': $p.width(),
                                'h': $p.height(),
                                'x': e.pageX,
                                'y': e.pageY
                            };

                            $.extend(document, {
                                'move': true,
                                'call_down': function (e) {
                                    $p.css({
                                        'width': Math.max(30, posix
                                            .x - e.pageX + posix
                                                .w),
                                        'height': Math.max(30, e
                                            .pageY - posix.y +
                                            posix.h),
                                        'left': Math.max(30, e
                                            .pageX)
                                    });
                                }
                            });
                        }
                    });
                } else if (y < (t + h) && y > (t + h - b) && x < (l + w) && x > (l + w - b)) {
                    $(this).css({
                        cursor: "se-resize"
                    });
                    $(this).unbind("mousedown").bind({
                        "mousedown": function (e) {
                            var $p = $(this);
                            var posix = {
                                'w': $p.width(),
                                'h': $p.height(),
                                'x': e.pageX,
                                'y': e.pageY
                            };
                            $.extend(document, {
                                'move': true,
                                'call_down': function (e) {
                                    $p.css({
                                        'width': Math.max(30, e
                                            .pageX - posix.x +
                                            posix.w),
                                        'height': Math.max(30, e
                                            .pageY - posix.y +
                                            posix.h)
                                    });
                                }
                            });
                        }
                    });
                } else if (y < (t + ww) && x > l && x < (l + w)) {
                    $(this).css({
                        cursor: "move"
                    });
                    $(this).unbind("mousedown");
                }
            },
            "mouseup": function () {
                data = window[`_dc_drop_${$(this)[0].id}`];

                if (hasHandle) {
                    $("#" + data["handle"]).css({
                        cursor: "default"
                    });
                } else {
                    $("#" + data["container"]).css({
                        cursor: "default"
                    });
                }

                $(this).unbind("mousedown");

                window[`_dc_drop_${data.container}_top`] = parseInt($(this).css("top").replace("px", ""));
                window[`_dc_drop_${data.container}_left`] = parseInt($(this).css("left").replace("px", ""));

                up_data = {
                    top: $(this).css("top"),
                    left: $(this).css("left"),
                    width: $(this).css("width"),
                    height: $(this).css("height"),
                };
                if (data["up"]) {
                    data["up"]({
                        node: this,
                        data: up_data,
                    });
                }
                if (data.is_log_position) {
                    // 保存大小及位置
                    localStorage[`pos_${$(this)[0].id}`] = JSON.stringify(up_data);
                }
            }
        });

        function resizeBox($p, posix, e) {
            $.extend(document, {
                'move': true,
                'call_down': function (e) {
                    $p.css({
                        'width': Math.max(30, e.pageX - posix.x + posix.w),
                        'height': Math.max(30, e.pageY - posix.y + posix.h)
                    });
                }
            });
        }
    }
}